package com.example.myapplication.view.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;

import com.example.myapplication.R;
import com.example.myapplication.bean.GoodsBean;
import com.example.myapplication.view.DetailActivity;
import com.example.myapplication.view.DetailView;
import com.example.myapplication.view.RefactorView.RefreshListView;
import com.example.myapplication.presenter.adapter.HotListAdapter;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {
    private String TAG = "HomeFragment";
    private List<Integer> swiperRes;
    private HomeViewModel mViewModel;
    private static HomeFragment fragment = null;
    private Context mContext;
    private RefreshListView listView;

    public static HomeFragment newInstance() {
        if (HomeFragment.fragment == null) {
            HomeFragment.fragment = new HomeFragment();
        }
        return HomeFragment.fragment;
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.home_fragment, container, false);
        initView(v);
        setData(v);
        return v;
    }

    private void initView(View v) {
        listView = v.findViewById(R.id.hot_list);
    }

    private void setData(View v) {
        mContext = v.getContext();
        swiperRes = new ArrayList<>();
        swiperRes.add(R.drawable.swiper);
        swiperRes.add(R.drawable.swiper1);
        swiperRes.add(R.drawable.swiper2);
        //hotList
        List<Object> list = new ArrayList<Object>();
        list.add(swiperRes);
        for (int i = 0; i < 5; i++) {
            GoodsBean g = new GoodsBean();
            g.setName("姓名" + i + 1);
            g.setPrice(100 * i);
            g.setUrl(R.drawable.shalaminyu);
            g.setSales(10 * i);
            list.add(g);
        }

        listView.setAdapter(new HotListAdapter(list, mContext));
//        listView.refl
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0) {
                    return;
                }
                startActivity(new Intent(mContext, DetailActivity.class));
            }
        });
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(HomeViewModel.class);
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroy() {
//        hd.removeCallbacks(ru);
        super.onDestroy();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

}
